package com.sc.pattern.activeobjects;

import java.util.LinkedList;

/**
 * Created  on 2018/1/14.
 *
 * @author sc
 */
public class ActivationQueue {
    private final static int MAX_QUEUE_SIZE = 100;
    private final LinkedList<MethodRequest> methodQ = new LinkedList<>();

    public synchronized void put(MethodRequest request){
        while (methodQ.size()>=MAX_QUEUE_SIZE){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.methodQ.addLast(request);
        this.notifyAll();
    }

    public synchronized MethodRequest take(){
        while (methodQ.isEmpty()){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        MethodRequest request = methodQ.removeFirst();
        this.notifyAll();
        return  request;
    }
}
